import { Effect, Reducer } from 'umi';
import {
     getArticleDetail,
    getArticlesList, getArticleSwipers,
    getArticleFe, getArticleBe, getArticleReading,
    getArticleLinux, getArticleLeetcode, getArticleNews,
    getArticleJava,gitArticleNavList, getArticleState, getArticleRecommend,
    
} from '@/services';
import {IArticleDetail, IArticleItem, IArticleSwiper, IArticleNavList, IArticleState } from '@/types';
// 接口
export interface ArticleModelState {
  
    articleList: IArticleItem[];
    articleLength: number;
    articleDetail: Partial<IArticleDetail>; // 把接口的属性变成可选的
    articleSwiper: IArticleSwiper[],
    getArticleNavList: IArticleNavList[];
    getArticleFe: IArticleItem[]
  //文章标签
  articleState: IArticleState[],
  articleComment: IArticleItem[]
  recommedArticleList:IArticleItem[]


}

export interface ArticleModelType {
    namespace: 'article';
    state: ArticleModelState;
    effects: {

        getArticlesList: Effect;
        getArticleDetail: Effect;
        getArticleSwipers: Effect;
        getArticleNavList: Effect;
        getArticleFe: Effect;
        getArticleBe: Effect;
        getArticleReading: Effect;
        getArticleLinux: Effect;
        getArticleLeetcode: Effect;
        getArticleNews: Effect;
        getArticleJava: Effect;
        getArticleState: Effect;
        getArticleRecommend: Effect;
        getRecommend: Effect;
    };
    reducers: {
        save: Reducer<ArticleModelState>;
    };

}

const ArticleModel: ArticleModelType = {
    // 命名空间  等价与 vuex的namespce,相当于redux的子reduce
    namespace: 'article',
    // 定义的转态
    state: {

        articleList: [],
        articleLength: 0,
        articleDetail: {},
        articleSwiper: [],
        getArticleNavList: [],
        getArticleFe: [],
        articleState: [],
        articleComment: [],
        recommedArticleList:[]
    },
    // 副作用,对应的异步操作,常指网络请求
    effects: {
        *getArticlesList({ payload }, { call, put }) {
            const result = yield call(getArticlesList, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        articleList: result.data[0],
                        articleLength: result.data[1]
                    },

                })
            }

        },
        *getArticleDetail({ payload }, { call, put }) {
            let result = yield call(getArticleDetail, payload);
            localStorage.setItem('Floor', result.data.toc)
      
            if (result.data) {
              yield put({
                type: 'save',
                payload: {
                  articleDetail: result.data
                }
              })
            }
          },
        //轮播
        *getArticleSwipers({ payload }, { call, put }) {
            let result = yield call(getArticleSwipers, payload);

            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        articleSwiper: result.data
                    }
                })
            }
        },

        *getArticleNavList({ payload }, { call, put }) {
            let result = yield call(gitArticleNavList, payload);
            if (result.data) {
              yield put({
                type: 'save',
                payload: {
                  getArticleNavList: result.data,
               
                }
              })
            }
          },
        // nav--前端数据
        *getArticleFe({ payload }, { call, put }) {
            console.log('payload-----------------',payload);

            let result = yield getArticleFe(payload)
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
        // nav--后端数据
        *getArticleBe({ payload }, { call, put }) {
            let result = yield call(getArticleBe, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
        // nav--阅读数据
        *getArticleReading({ payload }, { call, put }) {
            let result = yield call(getArticleReading, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
        // nav--Linux数据
        *getArticleLinux({ payload }, { call, put }) {
            let result = yield call(getArticleLinux, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
        // nav--Leetcode数据
        *getArticleLeetcode({ payload }, { call, put }) {
            let result = yield call(getArticleLeetcode, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
        // nav--News数据
        *getArticleNews({ payload }, { call, put }) {
            let result = yield call(getArticleNews, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
        // nav--Java数据
        *getArticleJava({ payload }, { call, put }) {
            let result = yield call(getArticleJava, payload);
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        getArticleFe: result.data[0],
                    }
                })
            }
        },
            //文章推荐
    *getArticleState({ payload }, { call, put }) {
        const result = yield call(getArticleState)
        if (result.data) {
          yield put({
            type: "save",
            payload: {
              articleState: result.data,
            }
          })
        }
      },
      *getArticleRecommend({ payload }, { call, put }) {
        const result = yield call(getArticleRecommend)
        if (result.data) {
          yield put({
            type: "save",
            payload: {
              articleComment: result.data,
            }
          })
        }
      },

      *getRecommend({payload},{call,put}){
        const result=yield call(getArticlesList)
        if(result.data){
          yield put({
            type:'save',
            payload:{
              recommedArticleList:result.data[0]
            }
          })
        }
      }


    },
    // 同步修改state的操作 
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },

    },

};

export default ArticleModel;